package com.example.simin_manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.simin_manage.admim.dto.Response;
import com.example.simin_manage.entity.PerformancePeriod;
import com.example.simin_manage.mapper.PerformancePeriodMapper;
import com.example.simin_manage.util.PerformancePeriodStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @description:
 * @author: panpan
 * @time: 2025/9/15
 */
@RestController
@RequestMapping("/api/periods")
public class PerformancePeriodController {

    @Resource
    private PerformancePeriodMapper performancePeriodMapper;

    // 获取所有周期
    @GetMapping
    public Response list() {
        List<PerformancePeriod> list = performancePeriodMapper.selectList(null);
        return new Response(200, "获取成功", list);
    }

    // 获取当前周期
    @GetMapping("/current")
    public Response current() {
        LocalDate today = LocalDate.now();
        PerformancePeriod current = performancePeriodMapper.selectOne(
                new LambdaQueryWrapper<PerformancePeriod>()
                        .le(PerformancePeriod::getStartDate, today)
                        .ge(PerformancePeriod::getEndDate, today)
        );
        if (current == null) {
            return new Response(404, "当前没有周期", null);
        }
        return new Response(200, "获取成功", current);
    }

    // 获取单个周期
    @GetMapping("/{id}")
    public Response get(@PathVariable Long id) {
        PerformancePeriod p = performancePeriodMapper.selectById(id);
        return p != null ? new Response(200, "获取成功", p) : new Response(404, "未找到", null);
    }

    // 新增周期
    @PostMapping
    public Response add(@RequestBody PerformancePeriod p) {
        p.setCreateTime(LocalDateTime.now());
        p.setUpdateTime(LocalDateTime.now());
        // 这里 createdBy 已经在 JSON 里传过来了
        performancePeriodMapper.insert(p);
        return new Response(200, "新增成功", p);
    }


    // 修改周期
    @PutMapping("/{id}")
    public Response update(@PathVariable Long id, @RequestBody PerformancePeriod p) {
        PerformancePeriod db = performancePeriodMapper.selectById(id);
        if (db == null) {
            return new Response(404, "未找到", null);
        }
        p.setId(id);
        p.setUpdateTime(LocalDateTime.now());
        performancePeriodMapper.updateById(p);
        return new Response(200, "修改成功", p);
    }

    // 删除周期
    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Long id) {
        int rows = performancePeriodMapper.deleteById(id);
        return rows > 0 ? new Response(200, "删除成功", null) : new Response(404, "未找到", null);
    }

    // 工具方法：根据日期判断状态
    private PerformancePeriodStatus determineStatus(LocalDate start, LocalDate end) {
        LocalDate today = LocalDate.now();
        if (today.isBefore(start)) {
            return PerformancePeriodStatus.NOT_STARTED;
        } else if (!today.isAfter(end)) {
            return PerformancePeriodStatus.ACTIVE;
        } else {
            return PerformancePeriodStatus.ENDED;
        }
    }
}